import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/16
 * Time:11:03
 */
public class Solution {
//    public static int[] searchRange(int[] nums, int target) {
//        if (nums == null) {
//            return new int[]{-1, -1};
//        }
//        int flg = -1;
//        //判断数组中是否存在target
//        for (int i = 0; i < nums.length; i++) {
//            if(nums[i] == target) {
//                flg = i;
//            }
//        }
//        //如果不存在,返回new int[]{-1, -1}
//        if (flg == -1) {
//            return new int[]{-1, -1};
//        }
//        //
//        int[] ret = new int[2];
//        int leftcount = 0;
//        for (int i = 0; i < nums.length; i++) {
//            if (nums[i] == target) {
//                leftcount++;
//                //ret[0]只保存第一个target
//                if (leftcount == 1) {
//                    ret[0] = i;
//                }
//            }
//            if (nums[i] == target) {
//                ret[1] = i;
//            }
//        }
//        return ret;
//    }
//    public static void main(String[] args) {
//        int[] ints = searchRange(new int[]{2,2}, 2);
//        System.out.println(Arrays.toString(ints));
//    }

    public int[] searchRange(int[] nums, int target){
        if (nums.length == 0 || nums[0] > target || nums[nums.length - 1] < target) {
            return new int[]{-1,-1};
        }
        //找到左边界
        int left = 0, right = nums.length - 1,mid = nums.length / 2;
        while(left < right){
            if(nums[mid] < target){
                left = mid + 1;
            }else {
                right = mid;
            }
            mid = left + (right - left) / 2;
        }
        int begin = left;
        //找到右边界
        left = 0;
        right = nums.length - 1;
        mid = nums.length / 2;
        while(left < right){
            if(nums[mid] <= target){
                left = mid;
            }else {
                right = mid - 1;
            }
            mid = left + (right - left + 1) / 2;
        }
        if (begin > right){
            return new int[]{-1,-1};
        }
        return new int[]{begin, right};
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int[] ret = s.searchRange(new int[]{5,6,7,8,8,10}, 6);
        System.out.println(Arrays.toString(ret));
    }
}
